import { history } from 'umi';
import { getKeyRouter } from '@/utils';
import { getDocumentationManageTableColumns, getDocumentationManageSearchColumns, getDocumentationManageTableHeaderActions } from '../mconfig/index';
import { DocumentationManageService } from '../services/ManageService';
import { useState } from 'react';

export function useDocumentationManage(curRes, locationProps) {
  const prefixCls = 'documentatio-manage';

  /**撤销弹窗*/
  const [invalidState, setInvalidState] = useState<Record<string, any>>({});

  const { reloadRequests, getDocumentationRun, getDocumentationLoading, reloadRequest, dataSource, ManageTableProps } = DocumentationManageService(curRes);

  /**
   * @operation
   * @description 管理页操作事件
   */
  const operation = {
    onEdit: (record) => {
      history.push({
        pathname: `/purchaseSourceController/editSource/:${getKeyRouter()}${getKeyRouter()}`,
        state: {
          type: 'edit',
          info: { ...record, applyOrderID: record?.applyID }, //id: record?.sourceID
          returnPathName: locationProps?.pathname,
        },
      });
    },
    onView: (record) => {
      // 跳转详情
      history.push({
        pathname: `/purchaseSourceController/getSourceById/{id}/{currentUserID}/:${getKeyRouter()}${getKeyRouter()}`,
        state: {
          type: 'view',
          info: { ...record, applyOrderID: record?.applyID },
          returnPathName: locationProps?.pathname,
        },
      });

      // 跳转OA
      // history.push({
      //   pathname: `/purchaseSourceController/dealHomePage/:${getKeyRouter()}${getKeyRouter()}`,
      //   state: {
      //     pageType: 'detail',
      //     id: record?.id,
      //     pageInfo: { id: record?.id },
      //     taskId: record?.id,
      //     pIDDetailInfo: { id: record?.id },
      //   },
      // });
    },
    onInvalid: (record) => {
      setInvalidState({ isShow: true, resObj: record });
    },
  };

  const tableProps = {
    columns: getDocumentationManageTableColumns(operation, curRes),
    dataSource: dataSource,
    loading: getDocumentationLoading,
    pagination: ManageTableProps?.pagination,
    rowKey: 'id',
    onPageChange: (current: number, pageSize: number, params) => getDocumentationRun({ current, pageSize, ...params }),
  };

  const searchProps = {
    switchList: [],
    onSwitch: (_: string | undefined | number) => {},
    searchList: getDocumentationManageSearchColumns(),
    onSearch: (request) => reloadRequest({ ...request }),
    onReset: () => reloadRequest({}),
  };

  const actionList = getDocumentationManageTableHeaderActions(operation);

  return {
    operation,
    tableProps,
    searchProps,
    actionList,
    prefixCls,
    state: {
      invalidState,
    },
    change: {
      setInvalidState,
      reloadRequests,
    },
  };
}
